<?php
namespace ZHMVC\D;

class Menu extends \ZHMVC\D\DataBase
{
    
    public function getOneByBiaoqianzhuiPid($biaoqianzhui)
    {
        $sql="select * from zhmvc_admin_menu where menuname=:menuname and pid='0'";
        $bind=array(":menuname"=>$biaoqianzhui);
        $data=$this->_db->getOne($sql,$bind);
        $this->_rows=$this->_db->getRowCount();
        return $data;
    }
    
    public function getOneByBiaoqianzhuiAll($biaoqianzhui)
    {
        $sql="select * from zhmvc_admin_menu where menuname=:menuname and pid<>'0'";
        $bind=array(":menuname"=>$biaoqianzhui);
        $data=$this->_db->getAll($sql,$bind);
        $this->_rows=$this->_db->getRowCount();
        return $data;
    }
    
    public function getAllNum($parameter="")
    {
        if(is_array($parameter)==true)
        {
            $bind=array();
            $s="";
            $tempS=parent::parseSql($parameter);
            $bind=$tempS["bind"];
            $s=$tempS["s"];
            
            if($s=="")
            {
                $sql="select count(*) as num from zhmvc_admin_menu";
                $data=$this->_db->getOne($sql);
            }
            else
            {
                //去掉第一个逻辑符号
                $s_a=explode(" ", $s);
                $tempS="";
                for($j=1;$j<count($s_a);$j++)
                {
                    $tempS.=$s_a[$j]." ";
                }
                
                $sql="select count(*) as num from zhmvc_admin_menu where ".$tempS;
                $data=$this->_db->getOne($sql,$bind);
            }
        }
        elseif($parameter!="")
        {
            $sql="select count(*) as num from zhmvc_admin_menu where ".$parameter;
            $data=$this->_db->getOne($sql);
        }
        else
        {
            $sql="select count(*) as num from zhmvc_admin_menu";
            $data=$this->_db->getOne($sql);
        }
        if(empty($data)==true)
        {
            $data["num"]=0;
        }
        $this->_rows=$this->_db->getRowCount();
        return $data;
    }
    
    public function getAll($parameter="")
    {
        if(is_array($parameter)==true)
        {
            $bind=array();
            $s="";
            $tempS=parent::parseSql($parameter);
            $bind=$tempS["bind"];
            $s=$tempS["s"];
            
            if($s=="")
            {
                $sql="select * from zhmvc_admin_menu";
                $datas=$this->_db->getAll($sql);
            }
            else
            {
                $s_a=explode(" ", $s);
                $tempS="";
                for($j=1;$j<count($s_a);$j++)
                {
                    $tempS.=$s_a[$j]." ";
                }
                $sql="select * from zhmvc_admin_menu where ".$tempS;
                $datas=$this->_db->getAll($sql,$bind);
            }
        }
        elseif($parameter!="")
        {
            $sql="select * from zhmvc_admin_menu where ".$parameter."";
            $datas=$this->_db->getAll($sql);
        }
        else
        {
            $sql="select * from zhmvc_admin_menu";
            $datas=$this->_db->getAll($sql);
        }
        
        $this->_rows=$this->_db->getRowCount();
        return $datas;
    }
    
    public function getRows()
    {
        return $this->_rows;
    }
    
    public function getPages($limit,$parameter="")
    {
        if(is_array($parameter)==true)
        {
            $bind=array();
            $s="";
            $tempS=parent::parseSql($parameter);
            $bind=$tempS["bind"];
            $s=$tempS["s"];
            if($s=="")
            {
                $sql="select * from zhmvc_admin_menu order by id desc LIMIT ".$limit."";
                $datas=$this->_db->getAll($sql);
            }
            else
            {
                $s_a=explode(" ", $s);
                $tempS="";
                for($j=1;$j<count($s_a);$j++)
                {
                    $tempS.=$s_a[$j]." ";
                }
                $sql="select * from zhmvc_admin_menu where ".$tempS." order by id desc LIMIT ".$limit."";
                $datas=$this->_db->getAll($sql,$bind);
            }
        }
        elseif($parameter!="")
        {
            $sql="select * from zhmvc_admin_menu where ".$parameter." order by id desc LIMIT ".$limit."";
            $datas=$this->_db->getAll($sql);
        }
        else
        {
            $sql="select * from zhmvc_admin_menu order by id desc LIMIT ".$limit."";
            $datas=$this->_db->getAll($sql);
        }
        $this->_rows=$this->_db->getRowCount();
        return $datas;
    }
    
    public function getOne($postid)
    {
        $sql="select * from zhmvc_admin_menu where id=:id";
        $bind=array(":id"=>$postid);
        $data=$this->_db->getOne($sql,$bind);
        $this->_rows=$this->_db->getRowCount();
        return $data;
    }
    
    public function getOne1($parameter="")
    {
        if(is_array($parameter)==true)
        {
            $bind=array();
            $s="";
            $tempS=parent::parseSql($parameter);
            $bind=$tempS["bind"];
            $s=$tempS["s"];
            
            if($s=="")
            {
                $sql="select * from zhmvc_admin_menu";
                $datas=$this->_db->getOne($sql);
            }
            else
            {
                $s_a=explode(" ", $s);
                $tempS="";
                for($j=1;$j<count($s_a);$j++)
                {
                    $tempS.=$s_a[$j]." ";
                }
                $sql="select * from zhmvc_admin_menu where ".$tempS;
                $datas=$this->_db->getOne($sql,$bind);
            }
        }
        elseif($parameter!="")
        {
            $sql="select * from zhmvc_admin_menu where ".$parameter."";
            $datas=$this->_db->getOne($sql);
        }
        else
        {
            $sql="select * from zhmvc_admin_menu";
            $datas=$this->_db->getOne($sql);
        }
        
        $this->_rows=$this->_db->getRowCount();
        return $datas;
    }
    
    public function add($pid, $menuname, $title, $url)
    {
        $IdWorker = \wantp\Snowflake\IdWorker::getIns();
        $id = $IdWorker->id();
        $sql = "insert into zhmvc_admin_menu(id,pid,menuname,title,url) values ('".$id."',:pid,:menuname,:title,:url)";
        $bind = array(
            ":pid" => $pid,
            ":menuname" => $menuname,
            ":title" => $title,
            ":url" => $url
        );
        $this->_db->update($sql, $bind);
        $this->_lastid = $id;
        return 1;
    }

    public function update($id, $pid, $menuname, $title, $url)
    {
        
        $sql = "update zhmvc_admin_menu set pid=:pid,menuname=:menuname,title=:title,url=:url where id=:id";
        $bind = array(
            ":pid" => $pid,
            ":menuname" => $menuname,
            ":title" => $title,
            ":url" => $url,
            ":id" => $id
        );
        $this->_db->update($sql, $bind);
        return 1;
    }

    public function delete($id)
    {
        $sql = "delete from zhmvc_admin_menu  where id=:id";
        $bind = array(
            ":id" => $id
        );
        $this->_db->update($sql, $bind);
        return 1;
    }

    public function getLastId()
    {
        return $this->_lastid;
    }
    
    public function addOne()
    {
        $IdWorker = \wantp\Snowflake\IdWorker::getIns();
        $id = $IdWorker->id();
        $sql = "insert into zhmvc_admin_menu(`id`) values (:id)";
        //echo $sql;
        $bind = array(":id" => $id);
        $this->_db->update($sql, $bind);
        $this->_lastid = $id;
        return 1;
    }
}
